package at.fhj.swd.pse.gui.pageobjects;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;

import at.fhj.swd.pse.gui.drivers.GuiTestBaseSeleniumJava4Webdriver;

/**
 * 
 * Get login page and specify tests to be performed on it here to be called.
 * 
 * @author Robert Angerer
 * 
 */
public class LoginPage extends GuiTestBaseSeleniumJava4Webdriver {
	private final GuiTestBaseSeleniumJava4Webdriver webdriver = new GuiTestBaseSeleniumJava4Webdriver();
	
	public GuiTestBaseSeleniumJava4Webdriver getWebdriver() {
		return webdriver;
	}

	public LoginPage() throws Exception {
		this.open();
	}

	public void open() throws Exception {
		// this.webdriver = new GuiTestBaseSeleniumJava4Webdriver();
		this.webdriver.start();
		this.webdriver.getDriver().get(this.webdriver.getBaseUrl() + "/login.xhtml");
		this.webdriver.getDriver().findElement(By.linkText("Login")).click();
	}

	public void close() throws Exception {
		this.webdriver.end();
	}

	public boolean loginWithUser(final String username, final String password, final String regExpSearchString) throws Exception {
		this.webdriver.getDriver().findElement(By.id("loginpage:username")).clear();
		this.webdriver.getDriver().findElement(By.id("loginpage:username")).sendKeys(username);
		this.webdriver.getDriver().findElement(By.id("loginpage:password")).clear();
		this.webdriver.getDriver().findElement(By.id("loginpage:password")).sendKeys(password);
		this.webdriver.getDriver().findElement(By.id("loginpage:loginButton")).click();
		  
		// BODY occurs too fast sometimes, login has not finished - for that reason a named tag.
		// WebElement dynamicElement = (new WebDriverWait(this.webdriver.getDriver(), 10))
		//		  .until(ExpectedConditions.presenceOfElementLocated(By.id("loginOutputLabel")));
		
		this.webdriver.getDriver().manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
		String loggedInUser = this.webdriver.getDriver().findElement(By.xpath("//a[contains(@id, 'userNameLoggedIn')]")).getText();
		
		// assertTrue(this.webdriver.getDriver().findElement(By.cssSelector("BODY")).getText().matches("Hello SWD11 Portal ! portalUser"));
		return username.equals(loggedInUser);
	}
	
	/*
	 * author: Bernadette Pecovnik
	 * 
	 * TODO Refactoring: General page object with a user logged-in should be available for alle selenium tests (design?) !!
	 */
	public boolean addActivity(final String activityText) throws Exception {
		
		// ids are generated dynamically
		// firefox plugin xpath checker: xpath is shown
		
		webdriver.getDriver().findElement(By.xpath("//input[contains(@id, 'ActivityStream:newActivityText')]")).clear();
		webdriver.getDriver().findElement(By.xpath("//input[contains(@id, 'ActivityStream:newActivityText')]")).sendKeys(activityText);
	
		webdriver.getDriver().findElement(By.xpath("//button[contains(@id, 'ActivityStream:addActivity')]")).click();
		
		String lastActivityInTable = webdriver.getDriver().findElement(By.xpath("//span[contains(text(), '"+ activityText+ "')]")).getText();
		
		// check if activity was added to table
		return lastActivityInTable.contains(activityText);
		
	}
	
}
